﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;

namespace NetTopologySuite.Geometries
{
	public class CoordinateList : List<Coordinate>, ICloneable
	{
		public CoordinateList()
		{
		}

		public CoordinateList(Coordinate[] coord) : base(coord.Length)
		{
			this.Add(coord, true);
		}

		public CoordinateList(IList<Coordinate> coordList) : base(coordList.Count)
		{
			this.AddAll(coordList, true);
		}

		public CoordinateList(IList<Coordinate> coordList, bool allowRepeated)
		{
			this.AddAll(coordList, allowRepeated);
		}

		public CoordinateList(Coordinate[] coord, bool allowRepeated)
		{
			this.Add(coord, allowRepeated);
		}

		public Coordinate GetCoordinate(int i)
		{
			return base[i];
		}

		public bool Add(Coordinate[] coord, bool allowRepeated, int start, int end)
		{
			int num = 1;
			if (start > end)
			{
				num = -1;
			}
			for (int num2 = start; num2 != end; num2 += num)
			{
				this.Add(coord[num2], allowRepeated);
			}
			return true;
		}

		public bool Add(Coordinate[] coord, bool allowRepeated, bool direction)
		{
			if (direction)
			{
				for (int i = 0; i < coord.Length; i++)
				{
					this.Add(coord[i], allowRepeated);
				}
			}
			else
			{
				for (int j = coord.Length - 1; j >= 0; j--)
				{
					this.Add(coord[j], allowRepeated);
				}
			}
			return true;
		}

		public bool Add(Coordinate[] coord, bool allowRepeated)
		{
			return this.Add(coord, allowRepeated, true);
		}

		public bool Add(object obj, bool allowRepeated)
		{
			return this.Add((Coordinate)obj, allowRepeated);
		}

		public bool Add(Coordinate coord, bool allowRepeated)
		{
			if (!allowRepeated && base.Count >= 1 && base[base.Count - 1].Equals2D(coord))
			{
				return false;
			}
			base.Add(coord);
			return true;
		}

		public void Add(int i, Coordinate coord, bool allowRepeated)
		{
			if (!allowRepeated)
			{
				int count = base.Count;
				if (count > 0)
				{
					if (i > 0 && base[i - 1].Equals2D(coord))
					{
						return;
					}
					if (i < count && base[i].Equals2D(coord))
					{
						return;
					}
				}
			}
			base.Insert(i, coord);
		}

		public bool AddAll(IList<Coordinate> coll, bool allowRepeated)
		{
			bool result = false;
			foreach (Coordinate coord in coll)
			{
				this.Add(coord, allowRepeated);
				result = true;
			}
			return result;
		}

		public void CloseRing()
		{
			if (base.Count > 0)
			{
				this.Add(new Coordinate(base[0]), false);
			}
		}

		public Coordinate[] ToCoordinateArray()
		{
			return base.ToArray();
		}

		public object Clone()
		{
			CoordinateList coordinateList = new CoordinateList();
			foreach (Coordinate coordinate in this)
			{
				coordinateList.Add((Coordinate)coordinate.Clone());
			}
			return coordinateList;
		}
	}
}
